#include<stdio.h>
#include<string.h>
#include<unistd.h>//pwd
#include<sstream>
#include<dirent.h>//list
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string>
using namespace std;
using std::cout;
using std::endl;
using std::cin;
string str;//zhi ling
string miling[1000];//bao cun zhi ling
int i;//zhi ling chang du
void menue1(){
	printf("please choose your choice:");
}
void pwd(){
	char pwd[100];
	getcwd(pwd,sizeof(pwd));//ba dang qian gong zuo de jue dui lu jing fu zhi dao pwd de nei cun kong jian
	cout<<pwd<<endl;
}
void input(){
	char local[100]="liyuchen@";
	char temp11[100];
	getcwd(temp11,sizeof(temp11));
	strcat(local,temp11);
	strcat(local,">");
	cout<<local;
	getline(cin,str);
	istringstream is(str);//jiang string gengju kongge fenkai baocun
	string temp;
	for(i=0;is>>temp;i++){
		miling[i]=temp;
	}
}
void list(){
	if(i==1){
		cout<<"loose mulu"<<endl;
	}
	else if(i==2){
		char temp[80];
		strcpy(temp,miling[1].c_str());//string -> cahr
		DIR *dir=NULL;
		struct dirent *end;
		int j=0;//feng 6 haun hang
		if((dir=opendir(temp))!=NULL){
			while(end=readdir(dir)){
				j++;
				if (strcmp(end->d_name,"..")!=0&&strcmp(end->d_name,".")!=0){
					cout<<end->d_name<<"  ";
				}
				if(j%10==0){
					cout<<endl;
				}
			}
			cout<<endl;
		}
		else{
			cout<<"there is no file or dilectory"<<endl;
		}
		closedir(dir);
	}
}	
void cd(){
	if(i>2){
		cout<<"error:number wrong"<<endl;
	}
	else{
		char temp[200];
		char temp1[200];
		getcwd(temp,sizeof(temp));
		if(i==1){
			
			char *a=strrchr(temp,'/');
			char *b=temp;
			int j=0;
			//printf("%d\n",sizeof(temp));
			for(j=0;j<a-b;j++){
				temp1[j]=temp[j];
			}
			for(j=a-b;j<200;j++){
				temp1[j]=NULL;
			}
			int c=a-b; 
			//printf("%s\n",temp);
			//printf("%d\n",a-b);
			//printf("%d\n",c);
			//printf("%s\n",temp1);
			int result=chdir(temp1);
			if(result!=0){
				printf("there is no file or dil\n");
			}
		}
		if(i==2){
			DIR *dir=NULL;
			strcat(temp,"/");
			strcat(temp,miling[1].c_str());
			if((dir=opendir(temp))!=NULL){
				printf("%s\n",temp);
				chdir(temp);
			}
			else{
				cout<<miling[1]<<"there is no file or dil "<<endl;
			}
			closedir(dir);
		}
	}
}
void mdir(){
	int a=0;
	a=mkdir(miling[1].c_str(),0777);
	if(a!=0){
		printf("can not do this,%s had already exist\n",miling[1].c_str());
	}
}
void redir(){
	int b=0;
	b=rmdir(miling[1].c_str());
	if(b!=0){
		printf("can not do this ,since there is no %s\n",miling[1].c_str());
	}
}
void rename1(){
	int c=0;
	c=rename(miling[1].c_str(),miling[2].c_str());
	if(c!=0){
		printf("you can not do this,since there is no %s\n",miling[1].c_str());
	}
}
int main(){
	printf("welcome\n");
	menue1();
	input();
	for(;;){
		if(strcmp(miling[0].c_str(),"pwd")==0){
			pwd();
		}
		if (strcmp(miling[0].c_str(),"list")==0){
			list();
		}
		if(strcmp(miling[0].c_str(),"cd")==0){
			cd();
		}
		if(strcmp(miling[0].c_str(),"mkdir")==0){
			mdir();
		}
		if(strcmp(miling[0].c_str(),"rmdir")==0){
			redir();
		}
		if(strcmp(miling[0].c_str(),"rename")==0){
			rename1();
		}
		if(strcmp(miling[0].c_str(),"exite")==0){
			break;
		}
		memset(miling,0,200);//memset cong shou di zhi kai shi chong xin fu zhi
		input();
	}
	return 0;
}
